home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
varia
/
egebook.lha
/
ege.book
/
3
/
inheritance.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-06-05
|
1KB
|
76 lines
#include <stdio.h>
#include "bool.h"
// Class definition for Person
class Person {
char* name;
public:
Person(); // constructor
Person(char *n); // bodies declared elsewhere
void print(); // member function
};
// Body for class Person
Person::Person(){ // constructor
name = "";
}
Person::Person(char *n){ // constructor
name = n;
};
void Person::print(){ // member functions
printf("my name is: %s \n", name);
};
// Class definition for Teacher
// Teacher is derived
class Teacher: public Person { // from Person
int courses;
static int MaxCourses;
public:
Teacher(char *name):Person(name){
courses = 0;
}
bool check();
void addCourse();
};
// Body for class Teacher
int Teacher::MaxCourses = 2;
bool Teacher::check(){
return (bool) (courses < MaxCourses);
}
void Teacher::addCourse() {
if (check())
courses++;
}
// Student class declaration
class Student: public Person{
public:
Student(char *name):Person(name){};
void print();
};
// Body for class Student
void Student::print(){
printf("Student: ");
Person::print();
}
main()
{
Teacher t("John Prof"); // constructor for Person
Student s("Hugo Meier"); // is invoked
t.print(); // Person's print
s.print(); // Student's print
}